package com.wude.guild.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.wude.bean.GiftInfo;
import com.wude.guild.GiftDetActivity;
import com.wude.guild.R;
import com.wude.guild.adapter.GameDetListGiftAdapter;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 游戏详情礼包页面Fragment
 * Created by Administrator on 2017/3/24.
 */

public class GameDetGiftFragment extends Fragment {

    @BindView(R.id.gift_list)
    ListView giftList;
    @BindView(R.id.error_text)
    TextView errorText;
    @BindView(R.id.error_layout)
    RelativeLayout errorLayout;
    private GameDetListGiftAdapter gameDetListGiftAdapter;
    private String id;
    List<GiftInfo> informationBeen = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_gamedet_gift, null);
        ButterKnife.bind(this, inflate);
        id = getArguments().getString("key");
        gameDetListGiftAdapter = new GameDetListGiftAdapter(getActivity());
        giftList.setAdapter(gameDetListGiftAdapter);
        Map<String, String> map = new HashMap<String, String>();
        map.put("game_id", id);
        HttpCom.POST(handler, HttpCom.getGameDetGiftUrl(), map, false);

        giftList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                List<GiftInfo> list = gameDetListGiftAdapter.getList();
                GiftInfo giftInfo = list.get(position);
                Intent intent = new Intent(getContext(), GiftDetActivity.class);
                intent.putExtra("id", giftInfo.gift_id + "");
                startActivity(intent);
            }
        });
        return inflate;
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("GameDetGiftFragment");
    }

    @Override
    public void onPause() {
        super.onPause();

        MobclickAgent.onPageEnd("GameDetGiftFragment");
    }


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    List<GiftInfo> giftInfos = HttpUtils.DNSGiftList(msg.obj.toString());
                    if (giftInfos != null) {
                        giftList.setVisibility(View.VISIBLE);
                        errorLayout.setVisibility(View.GONE);
                        informationBeen.addAll(giftInfos);
                        gameDetListGiftAdapter.setList(informationBeen);
                    } else {
                        giftList.setVisibility(View.GONE);
                        errorLayout.setVisibility(View.VISIBLE);
                        errorText.setText("该游戏暂无游戏礼包");
                    }
                    break;
                case 2:
                    giftList.setVisibility(View.GONE);
                    errorLayout.setVisibility(View.VISIBLE);
                    errorText.setText("网络异常");
                    break;
            }
        }
    };
}
